#include "messager.h"
#include "ams.h"
#include "log.h"
#include "lvgl.h"
#include "rtc_misc.h"

LV_IMG_DECLARE(icon_info);
LV_IMG_DECLARE(icon_gps);
LV_IMG_DECLARE(icon_capacity);
LV_IMG_DECLARE(icon_distance);
LV_IMG_DECLARE(icon_heart);
LV_IMG_DECLARE(icon_running);
LV_IMG_DECLARE(icon_cloudy);
LV_IMG_DECLARE(icon_alipay);

ACTIVITY_OPS_IMPLEMENT_DECLARE(applist, &icon_info, ACTIVITY_ATTR_FLAG_LIST);

// ##
static void app_gesture_handle(void *event)
{
    ams_gesture_t evt = *((ams_gesture_t *)event);
    switch(evt)
    {
        case GESTURE_SLIDE_TO_TOP:
            break;
        case GESTURE_SLIDE_TO_BOTTOM:
            break;
        case GESTURE_SLIDE_TO_LEFT:
            break;
        case GESTURE_SLIDE_TO_RIGHT:
            ams_switch_by_name("dial", DIR_SLIDE_TO_RIGHT);
            break;
    }
}

// ##
static void app_start(void *content)
{

}

// ##
static void app_stop(void *content)
{

}

// ##
static void list_event_handler(lv_obj_t *obj, lv_event_t event)
{
    if (event == LV_EVENT_CLICKED)
    {
        ams_switch_by_name((char *)lv_list_get_btn_text(obj), DIR_SLIDE_TO_LEFT);
    }
}

// ##
static void app_prepare(void *content)
{
    ams_activity_t *activity = (ams_activity_t *)content;
    AMS_UNUSE(activity);
}

// ##
static void *app_create(void *content)
{
    lv_obj_t *lv_screen = lv_obj_create(NULL, NULL);
    lv_obj_t *app_list = lv_list_create(lv_screen, NULL);
    lv_obj_set_size(app_list, LV_HOR_RES, LV_HOR_RES);
    lv_obj_align(app_list, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);

    ams_profile_node_t *link = NULL;
    ams_activity_get_list(&link);
    while (link != NULL)
    {
        ams_profile_node_t *new = link;
        lv_obj_t *btn = lv_list_add_btn(app_list, link->icon, link->name);
        lv_obj_set_event_cb(btn, list_event_handler);
        log_info("app [%s] added into list\r\n", link->name);
        link = link->next;
        ams_free(new);
    }

    return lv_screen;
}

// ##
static void *app_destroy(void *content)
{
    return NULL;
}

// ##
static void app_event_handle(void *event)
{

}
